/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses.DataRetrievalComponents;

import Beans.Requests.NetworkManagement.Components.HostNetworkConfiguration;
import Beans.Requests.NetworkManagement.Components.InternalNetworkConfiguration;
import Beans.VirtualResourceBean;
import Utilities.Constants.NetworkType;
import org.virtualbox_3_2.HostNetworkInterfaceType;

/**
 * This class represents network data, when retrieved from the system.
 *
 * @author Angel Sanadinov
 */
public class NetworkDataComponent extends DataRetrievalComponent
{
    private NetworkType networkType; //network type (host-only is for bridged as well)
    private InternalNetworkConfiguration internalNetwork; //internal network configuration
    private HostNetworkConfiguration hostNetwork;   //host network configuration
    private boolean isNetworkDataPartial;           //denotes whether network data is partial
    private String networkName;                     //the name of the network
    private VirtualResourceBean networkData;        //network data from the database
    private HostNetworkInterfaceType interfaceType; //interface type (bridged and host-only)

    /**
     * Constructs a network data component, using the specified internal network data. <br><br>
     *
     * Used when the service retrieves a single network data object.
     *
     * @param internalNetwork the internal network data
     */
    public NetworkDataComponent(InternalNetworkConfiguration internalNetwork)
    {
        this.networkType = NetworkType.INTERNAL;
        this.internalNetwork = internalNetwork;
        this.hostNetwork = null;
        this.isNetworkDataPartial = false;
        this.networkName = null;
        this.networkData = null;
        this.interfaceType = null;
    }

    /**
     * Constructs a network data component, using the specified host network data. <br><br>
     *
     * Used when the service retrieves a single network data object.
     *
     * @param hostNetwork the host network data
     */
    public NetworkDataComponent(HostNetworkConfiguration hostNetwork)
    {
        this.networkType = NetworkType.HOST_ONLY;
        this.internalNetwork = null;
        this.hostNetwork = hostNetwork;
        this.isNetworkDataPartial = false;
        this.networkName = null;
        this.networkData = null;
        this.interfaceType = null;
    }

    /**
     * Constructs a network data component, using the specified data. <br><br>
     *
     * Used when the service retrieves multiple network data objects.
     *
     * @param networkName the name of the network
     * @param networkType the type of the network
     * @param networkData network data from the database
     * @param interfaceType the type of host interface (should be <code>null</code>
     *                      for internal networks)
     */
    public NetworkDataComponent(String networkName, NetworkType networkType,
                                VirtualResourceBean networkData, HostNetworkInterfaceType interfaceType)
    {
        this.internalNetwork = null;
        this.hostNetwork = null;
        this.isNetworkDataPartial = true;
        this.networkName = networkName;
        this.networkType = networkType;
        this.networkData = networkData;
        this.interfaceType = interfaceType;
    }

    /**
     * Retrieves the type of the network.
     *
     * @return the network type
     */
    public NetworkType getNetworkType()
    {
        return networkType;
    }

    /**
     * Retrieves the internal network configuration.
     *
     * @return the network configuration
     */
    public InternalNetworkConfiguration getInternalNetwork()
    {
        return internalNetwork;
    }

    /**
     * Retrieves the host network configuration.
     * 
     * @return the network configuration
     */
    public HostNetworkConfiguration getHostNetwork()
    {
        return hostNetwork;
    }

    /**
     * Returns whether the data in the component is partial or full.
     *
     * @return <code>true</code> if network data is partial or <code>false</code>
     *         if network data is full
     */
    public boolean isNetworkDataPartial()
    {
        return isNetworkDataPartial;
    }

    /**
     * Retrieves the name of the network. <br><br>
     *
     * <b>Note:</b> <i>The object should be validated before this method is called.</i>
     *
     * @return the network name
     */
    public String getNetworkName()
    {
        if(isNetworkDataPartial)
            return networkName;
        else if(networkType == NetworkType.INTERNAL)
            return internalNetwork.getInternalNetworkData().getNetworkName();
        else if(networkType == NetworkType.HOST_ONLY)
            return hostNetwork.getNetworkName();
        else
            return null;
    }

    /**
     * Retrieves the DHCP server availability for the network. <br><br>
     *
     * <b>Note:</b> <i>The object should be validated before this method is called.</i>
     *
     * @return <code>true</code> if a DHCP server is available or <code>false</code>
     *         otherwise
     */
    public boolean isDHCPAvailable()
    {
        if(networkType == NetworkType.INTERNAL)
            return internalNetwork.getInternalNetworkData().isDHCPAvailable();
        else if(networkType == NetworkType.HOST_ONLY)
            return hostNetwork.isDHCPAvailable();
        else
            return false;
    }

    /**
     * Retrieves the network data object. <br><br>
     *
     * <b>Note:</b> <i>The object should be validated before this method is called.</i>
     *
     * @return the network data
     */
    public VirtualResourceBean getNetworkData()
    {
        if(isNetworkDataPartial)
            return networkData;
        else if(networkType == NetworkType.INTERNAL)
            return internalNetwork.getNetworkData();
        else if(networkType == NetworkType.HOST_ONLY)
            return hostNetwork.getNetworkData();
        else
            return null;
    }

    /**
     * Retrieves the type of host interface. <br><br>
     * 
     * <b>Note:</b> <i>This is valid only for Host-Only and Bridged interface.</i>
     *
     * @return the interface type or <code>null</code> if none is available
     */
    public HostNetworkInterfaceType getInterfaceType()
    {
        if(isNetworkDataPartial)
            return interfaceType;
        else if(networkType == NetworkType.HOST_ONLY)
            return hostNetwork.getInterfaceType();
        else
            return null;
    }

    /**
     * Checks the validity of the partial object data.
     *
     * @return <code>true</code> if the data is valid or <code>false</code> if it is not
     */
    private boolean isPartialDataValid()
    {
        if(networkData != null && networkData.isValid() && networkName != null
           && networkType != null && networkType != NetworkType.INVALID_TYPE)
            return true;
        else
            return false;
    }

    @Override
    public boolean isValid()
    {
        if((isNetworkDataPartial && isPartialDataValid())
           || (networkType == NetworkType.INTERNAL && internalNetwork != null && internalNetwork.isValid())
           || (networkType == NetworkType.HOST_ONLY && hostNetwork != null && hostNetwork.isValid()))
            return true;
        else
            return false;
    }
}
